/*
 * 
 * JPress版微信小程序
 * 
 */
var jpress = require('../../utils/jpress.js');
var Auth = require('../../utils/auth.js');
var app = getApp();
Page({

  data: {    
    readLogs: [],
    topBarItems: [
        // id name selected 选中状态
        { id: '1', name: '浏览', selected: true },
        { id: '2', name: '评论', selected: false},
        { id: '3', name: '点赞', selected: false },
        { id: '4', name: '鼓励', selected: false },
        { id: '5', name: '订阅', selected: false },
        { id: '6', name: '言论', selected: false }
    ],
    tab: '1',
    showError: "none",
    shownodata:"none",
    subscription:"",
    userInfo:{},
    userLevel:{},
    openId:'',
    isLoginPopup: false  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
      wx.setNavigationBarTitle({
          title: app.globalData.appName +'我的',
          success: function (res) {
              // success
          }
      });
      var self = this;
      self.fetchPostsData('1');
      Auth.checkLogin(self);
      Auth.setUserInfoData(self);
  },
  onReady: function (){
      var self = this;
      Auth.checkSession(self);
  },
  agreeGetUser:function(e){
      let self= this;
      Auth.checkAgreeGetUser(e,app,self);
  },
  refresh:function(e){
      var self=this;
      if (self.data.openId) {
          var userInfo=e.detail.userInfo;
          let user = wx.getStorageSync('user');
          user.avatar= userInfo.avatarUrl;
          user.nickname = userInfo.nickName;
          user.address = userInfo.country + userInfo.province + userInfo.city;
          jpress.doUserSave(user).then(res => {
              wx.setStorageSync('userInfo',userInfo);
              self.setData({userInfo:userInfo});
              wx.showToast({
                  title: "更新成功",
                  icon: 'success',
                  duration: 900,
                  success: function () {

                  }
              })
          });
      }else {
        Auth.checkSession(self);
      }
  },
  exit:function(e){
      Auth.logout(this);
      wx.reLaunch({
          url: '../index/index'
      })
  },
  clear:function(e){
      Auth.logout(this);
  },

  // 跳转至查看文章详情
  redictDetail: function (e) {
      var id = e.currentTarget.id;
      var itemtype = e.currentTarget.dataset.itemtype;
      var url ="";
      if (itemtype=="1"){
          url = '../list/list?categoryID=' + id;
      } else{
          url = '../detail/detail?id=' + id;
      }
      wx.navigateTo({
          url: url
      })
  },
  onTapTag: function (e) {
      var self = this;
      var tab = e.currentTarget.id;
      var topBarItems = self.data.topBarItems;
      // 切换topBarItem 
      for (var i = 0; i < topBarItems.length; i++) {
          if (tab == topBarItems[i].id) {
              topBarItems[i].selected = true;
          } else {
              topBarItems[i].selected = false;
          }
      }
      self.setData({
          topBarItems: topBarItems,
          tab: tab

      })
      if (tab !== 0) {
          this.fetchPostsData(tab);
      } else {
          this.fetchPostsData("1");
      }
  },
  onShareAppMessage: function () {
      var title = "分享我在“" + app.globalData.appName + "浏览、评论、点赞、鼓励的文章";
      var path = "pages/readlog/readlog";
      return {
          title: title,
          path: path,
          success: function (res) {
              // 转发成功
          },
          fail: function (res) {
              // 转发失败
          }
      }
  },
  fetchPostsData: function (tab) {
      self = this;
      self.setData({
          showError: 'none',
          shownodata:'none'
      }); 
      var count =0;
      var openId = "";
      if(tab !='1'){
          if (self.data.openId) {
              openId = self.data.openId;
          } else {
              Auth.checkSession(self);
              return;
          }
      }
      if (tab == '1'){
          self.setData({
              readLogs: (wx.getStorageSync('readLogs') || []).map(function (log) {
                  count++;
                  return log;
              })
          });
          if (count == 0) {
              self.setData({
                  shownodata: 'block'
              });
          }
      }else if (tab == '2'){
          self.setData({
              readLogs: []
          });
          jpress.getMyCommentArticles({
              openId:openId,
              source:'wechat_miniprogram'
          }).then(data => {
              if (data.page) {
                  self.setData({
                      readLogs: self.data.readLogs.concat(data.page.list.map(function (item) {
                          count++;
                          item[0] = item.article_id;
                          item[1] = item.title;
                          return item;
                      }))
                  });
                  if (count == 0) {
                      self.setData({
                          shownodata: 'block'
                      });
                  }
              }else {
                  console.log(data);
                  self.setData({
                      showError: 'block'
                  });
              }
          })
      }else if (tab == '3') {
          self.setData({
              readLogs: []
          });
          jpress.getMyLikeArticles({
              openId: openId,
          }).then(data => {
              if (data.list) {
                  self.setData({
                      readLogs: self.data.readLogs.concat(data.list.map(function (item) {
                          count++;
                          item[0] = item.id;
                          item[1] = item.title;
                          item[2] = "0";
                          return item;
                      }))
                  });

                  if (count == 0) {
                      self.setData({
                          shownodata: 'block'
                      });
                  } 
              }
              else {
                  console.log(data);
                  self.setData({
                      showError: 'block'
                  });

              }
          })
      }else if (tab == '4') {
          self.setData({
              readLogs: []
          });
          jpress.getMyPraisePosts({
              openId: openId,
          }).then(data => {
              if (data.list) {
                  self.setData({
                      readLogs: self.data.readLogs.concat(data.list.map(function (item) {
                          count++;
                          count++;
                          item[0] = item.id;
                          item[1] = item.title;
                          item[2] = "0";
                          return item;
                      }))
                  });
                  if (count == 0) {
                      self.setData({
                          shownodata: 'block'
                      });
                  }
              }else{
                  console.log(data);
                  this.setData({
                      showError: 'block'
                  });
              }
          })
      }else if (tab == '5') {
          self.setData({
              readLogs: []
          });
          jpress.getMySubPost({
              openId: openId,
          }).then(data => {
              if (data.list) {
                  self.setData({
                      readLogs: self.data.readLogs.concat(data.list.map(function (item) {
                          count++;
                          item[0] = item.id;
                          item[1] = item.title;
                          item[2] = "0";
                          return item;
                      }))
                  });
                  if (count == 0) {
                      self.setData({
                          shownodata: 'block'
                      });
                  }
              }else {
                  console.log(data);
                  this.setData({
                      showError: 'block'
                  });
              }
          })
      }else if (tab == '6'){
          self.setData({
              readLogs: []
          });
          jpress.getNewComments().then(data => {
              if (data.list) {
                  self.setData({
                      readLogs: self.data.readLogs.concat(data.list.map(function (item) {
                          count++;
                          item[0] = item.id;
                          item[1] = item.title;
                          item[2] = "0";
                          return item;
                      }))
                  });
                  if (count == 0) {
                      self.setData({
                          shownodata: 'block'
                      });
                  }
              }else {
                  console.log(data);
                  self.setData({
                      showError: 'block'
                  });

              }
          }).catch(function () {
              self.setData({
                  showError: 'block'
              });
          })
      }
  },  
  closeLoginPopup() {
      this.setData({ isLoginPopup: false });
  },
  openLoginPopup() {
      this.setData({ isLoginPopup: true });
  },
  confirm: function () {
      this.setData({
          'dialog.hidden': true,
          'dialog.title': '',
          'dialog.content': ''
      })
  }
})